import 'package:flutter/material.dart';
import 'package:qqjdflutter/config/jd_api.dart';
import 'package:qqjdflutter/model/category_content_model.dart';
import 'package:qqjdflutter/net/net_request.dart';

/// 分类
class CategoryPageProvider with ChangeNotifier{
  bool isLoading = false;
  bool isError = false;
  String errorMsg = "";
  List<String> categoryNavList = [];
  List<CategoryContentModel> categoryContentList = [];
  int tabIndex = 0;

  /// 分类左侧
  loadCategoryPageData(){
    isLoading = true;
    isError = false;
    errorMsg = "";
    NetRequest().reqeustData(JdApi.CATEGORY_NAV).then((res){
      isLoading = false;
      print("请求分类数据${res.data}");
      if(res.data is List){
        for(var i = 0;i< res.data.length;i++){
          categoryNavList.add(res.data[i]);
        }
        loadCategoryContentData(this.tabIndex);

      }
      notifyListeners();
    }).catchError((error){
      print(error);
      errorMsg = error;
      isLoading = false;
      isError = true;
      notifyListeners();

    });
  }
  loadCategoryContentData(int index){
    this.tabIndex = index;
    isLoading = true;
    categoryContentList.clear();

    /// 请求数据
    var data = {"title": categoryNavList[index]};
    NetRequest().reqeustData(JdApi.CATEGORY_CONTENT,data:data,method: "post").then((res){
      isLoading = false;
      print("右侧数据${res.data}");
      if(res.data is List){
        for(var item in res.data){
          CategoryContentModel tmpModel = CategoryContentModel.fromJson(item);
          categoryContentList.add(tmpModel);
        }
      }
      notifyListeners();
    }).catchError((error) {
      print(error);
      errorMsg = error;
      isLoading = false;
      isError = true;
      notifyListeners();
    });
    notifyListeners();
  }
}